﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SpeedTest {
    class Program {


        static void Main(string[] args) {
            Program p = new Program();
                //p.ListAdd();
                //p.SortedDictionaryAdd();
                //p.DictionaryAdd();
                //p.ListContains();
                //p.SortedDictionaryContains();
                //p.DictionaryContains();
                p.ListSortedContains();

        }

        public void ListAdd() {

            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputList.txt");
            try {      
                Stopwatch sw = new Stopwatch();
                List<int> things = new List<int>();

                sw.Start();
                for (int i = 0; i < 1000000000; i++) {
                    things.Add(i);
                    if (i % 1000000 == 0) {
                        streamW.WriteLine(ShowTime(sw.Elapsed) + ";" + things.Count);
                    }
                }
            }
            catch (OutOfMemoryException) {
                streamW.Flush();
                streamW.Close();
            }
        }

        public void SortedDictionaryAdd() {
            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputSortedDirectory.txt");
            try {
                Stopwatch sw = new Stopwatch();
                SortedDictionary<int, int> things = new SortedDictionary<int, int>();

                sw.Start();
                for (int i = 0; i < 1000000000; i++) {
                    things.Add(i, i);
                    if (i % 1000000 == 0) {
                        streamW.WriteLine(ShowTime(sw.Elapsed) + ";" + things.Count);
                        streamW.Flush();
                    }
                }
            }
            catch (OutOfMemoryException) {
                streamW.Flush();
                streamW.Close();
            }
        }

        public void DictionaryAdd() {
            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputDirectory.txt");
            try {
                Stopwatch sw = new Stopwatch();
                Dictionary<int, int> things = new Dictionary<int, int>();

                sw.Start();
                for (int i = 0; i < 1000000000; i++) {
                    things.Add(i, i);
                    if (i % 1000000 == 0) {
                        streamW.WriteLine(ShowTime(sw.Elapsed) + ";" + things.Count);
                    }
                }
            }
            catch (OutOfMemoryException) {
                streamW.Flush();
                streamW.Close();
            }
        }

        public void ListContains() {
            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputListContains.txt");
            Stopwatch sw = new Stopwatch();
            List<int> things = new List<int>();

            sw.Start();
            for (int i = 0; i < 1000000000; i++) {
                things.Contains(i);
                if (i % 10000000 == 0) {
                    streamW.WriteLine(ShowTime(sw.Elapsed) + ";" + i);
                }
            }
            streamW.Flush();
            streamW.Close();
        }

        public void SortedDictionaryContains() {
            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputSortedDirectoryContains.txt");
            Stopwatch sw = new Stopwatch();
            SortedDictionary<int, int> things = new SortedDictionary<int, int>();

            sw.Start();
            for (int i = 0; i < 1000000000; i++) {
                things.ContainsValue(i);
                if (i % 10000000 == 0) {
                    streamW.WriteLine(ShowTime(sw.Elapsed) + ";" + i);
                }
            }
            streamW.Flush();
            streamW.Close();
        }

        public void DictionaryContains() {
            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputDictionaryContains.txt");
            Stopwatch sw = new Stopwatch();
            Dictionary<int, int> things = new Dictionary<int, int>();

            sw.Start();
            for (int i = 0; i < 1000000000; i++) {
                things.ContainsValue(i);
                if (i % 10000000 == 0) {
                    streamW.WriteLine(ShowTime(sw.Elapsed) + ";" + i);
                }
            }
            streamW.Flush();
            streamW.Close();
        }

        public void ListSortedContains() {
            System.IO.StreamWriter streamW = new System.IO.StreamWriter("outputListSortedContains.txt");
            Stopwatch sw = new Stopwatch();
            List<int> things = new List<int>();
            sw.Start();
            things.Sort();
            for (int i = 0; i < 1000000000; i++) {
                things.BinarySearch(i);
                if (i % 10000000 == 0) {
                    streamW.WriteLine(ShowTime(sw.Elapsed) + ";" + i);
                }
            }
            streamW.Flush();
            streamW.Close();
        }

        private string ShowTime(TimeSpan ts) {
            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}.{1:000}", ts.Seconds, ts.Milliseconds);
            return elapsedTime;
        }
    }
}
